package mytest.reflect;

/**
 * Created by thinkpad on 2015/9/20.
 */
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Method;

public class CreateClass {
    private static String CLASS_NAME = "CreateClassTest";
    private static String CLASS_FILE = CLASS_NAME + ".java";

    public static void main(String args[]) {
        CreateClass mtc = new CreateClass();
        mtc.createClass();
        System.out.println("javac " + CLASS_FILE);
        mtc.compileClass();
        System.out.println("java " + CLASS_NAME);
        mtc.runClass();
    }

    public void createClass1() {
        try {
            new File(CLASS_FILE).delete();
            FileWriter aWriter = new FileWriter(CLASS_FILE, true);
            aWriter.write("public   class   " + CLASS_NAME + "{");
            aWriter.write("public   void   println()   { int x =5;");
//            aWriter.write("System.out.println("=" + CLASS_NAME + "=");");
            aWriter.write("System.out.println(x);");
            aWriter.write("}}");
            aWriter.flush();
            aWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void createClass() {
        try {
            new File(CLASS_FILE).delete();
            FileWriter aWriter = new FileWriter(CLASS_FILE, true);
            StringBuffer strBuffer = new StringBuffer();
            strBuffer.append("public   class  ")
                    .append(CLASS_NAME)
                    .append(" {")
                    .append("\t\n");
//            aWriter.write("public   class   " + CLASS_NAME + "{\t\n");
            strBuffer.append("public   void   println()")
                    .append("\t\n")
                    .append("{\t\n")
                    .append("int x = 5;")
                    .append("\t\n");
            strBuffer.append("System.out.println(x);")
                    .append("\t\n")
                    .append("}\t\n")
                    .append("}\t\n")
                    .append("\t\n");
//            aWriter.write("public   void   println()   {\t\n int x =5;\t\n");
//            aWriter.write("System.out.println("=" + CLASS_NAME + "=");");
//            aWriter.write("System.out.println(x);\t\n");
//            aWriter.write("}\t\n}\t\n");
            aWriter.write(strBuffer.toString());
            aWriter.flush();
            aWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void compileClass() {
        String filePath = new File(CreateClass.class.getClassLoader()
                .getResource("").getFile()).getAbsolutePath();
        String[] source = { "-d", filePath, new String(CLASS_FILE) };
        System.out.println("javac out:"
                + com.sun.tools.javac.Main.compile(source));
    }

    public void runClass() {
        try {
            Class params[] = {};
            Object paramsObj[] = {};
            Class testClass = Class.forName(CLASS_NAME);
            Object iClass = testClass.newInstance();
            Method thisMethod = testClass.getDeclaredMethod("println", params);
            thisMethod.invoke(iClass, paramsObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
